package com.tepestech.pfautocard.activity

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.AdapterView
import android.widget.Toast
import com.tepestech.pfautocard.R
import com.tepestech.pfautocard.util.CharacterAdapter
import com.tepestech.pfautocard.util.PlayerCharacterInfo
import kotlinx.android.synthetic.main.activity_role_selecting.*
import java.util.ArrayList


class RoleSelectingActivity : AppCompatActivity() {
    var selecting = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_role_selecting)
        setSupportActionBar(role_select_toolbar)    //为Activity配置Toolbar
        val list = ArrayList<PlayerCharacterInfo>()
        list.add(PlayerCharacterInfo(this, "Panty", "Fighter.v1", R.raw.ic_panty))
        list.add(PlayerCharacterInfo(this, "Mr.Quin", "Rogue.v1", R.raw.ic_mrquin))
        list.add(PlayerCharacterInfo(this, "PandaSan", "Monk.v1", R.raw.ic_panda))

        val adapter = CharacterAdapter(this)
        lv_role_list.adapter = adapter
        adapter.setData(list)
        lv_role_list.onItemClickListener = AdapterView.OnItemClickListener { _, _, po, _ ->
            when (list[po].name) {
                "Panty" -> startActivity(Intent(this@RoleSelectingActivity, MainActivity::class.java))
            }
        }
        lv_role_list.onItemLongClickListener = AdapterView.OnItemLongClickListener { _, _, position, _ ->
            Toast.makeText(this@RoleSelectingActivity, list[position].name, Toast.LENGTH_SHORT).show()
            selecting = true
            this@RoleSelectingActivity.invalidateOptionsMenu()
            return@OnItemLongClickListener true
        }
    }

    override fun onBackPressed() {
        if (selecting) {
            selecting = false
            invalidateOptionsMenu()
            return
        } else
            super.onBackPressed()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_role_selecting, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_add ->
                Toast.makeText(this@RoleSelectingActivity, "Add New Character", Toast.LENGTH_SHORT).show()
            R.id.action_delete ->
                Toast.makeText(this@RoleSelectingActivity, "Delete This Character", Toast.LENGTH_SHORT).show()
        }
        return true
    }

    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        menu!!.getItem(0)?.isVisible = !selecting
        menu.getItem(1)?.isVisible = selecting
        return super.onPrepareOptionsMenu(menu)
    }

}
